package com.app.controller;

import com.app.bean.Movies;
import com.app.bean.News;
import com.app.bean.Ticket;
import com.app.service.MoviesService;
import com.app.service.NewsService;
import com.app.util.Const;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 师晓峰
 * @version V1.0
 * @date 2023/9/24 22:11
 * @Description:
 *  前端页面
 */
@Controller
@RequestMapping("/front")
public class IndexController {

    @Autowired
    private MoviesService moviesService;

    @Autowired
    private NewsService newsService;

    /**
     * 首页
     * @param moviename
     * @return
     */
    @RequestMapping("/index")
    public ModelAndView index(String moviename){
        ModelAndView mav = new ModelAndView();
        PageHelper.startPage(1,5);
        List<Movies> moviesList = moviesService.hotList(moviename);
        // 正在热映
        mav.addObject("moviesList",moviesList);
        // 最受期待
        mav.addObject("isenjoy", moviesService.query("isenjoy"));
        // 右侧展示
        aside(mav);

        mav.setViewName("front/index.jsp");
        return mav;
    }

    /**
     * 注册用户退出
     * @param request
     * @return
     */
    @RequestMapping("/userLogout")
    public ModelAndView userLogout(HttpServletRequest request) {
        request.getSession().removeAttribute(Const.CURRENT_USER);
        return index("");
    }



    /**
     * 电影详情
     *  左侧详情 右侧不变
     * @param id
     * @return
     */
    @RequestMapping("/detail")
    public ModelAndView detail(String id){
        ModelAndView mav = new ModelAndView();
        PageHelper.startPage(1,5);
        // 通过id 获取电影信息
        Movies movies = moviesService.getById(id);
        mav.addObject("movies",movies);
        // 获取所有的电影票
        List<Ticket> ticketList = moviesService.getTicketsByMovieId(id);
        mav.addObject("ticketList",ticketList);
        aside(mav);
        mav.setViewName("front/detail.jsp");
        return mav;
    }

    /**
     * 新闻详情
     *  左侧详情 右侧不变
     * @param id
     * @return
     */
    @RequestMapping("/detailNews")
    public ModelAndView detailNews(String id){
        ModelAndView mav = new ModelAndView();
        PageHelper.startPage(1,5);
        // 通过id 获取电影信息
        News news = newsService.getById(id);
        mav.addObject("news",news);
        aside(mav);
        mav.setViewName("front/detailNews.jsp");
        return mav;
    }

    /**
     *  右侧展示
     *    // 最受期待
     *    // 好评如潮
     *    // 今日票房
     *    // 获取最新的三条新闻
     * @param mav
     */
    public void aside(ModelAndView mav) {
        // 好评如潮
        mav.addObject("isesteem", moviesService.query("isesteem"));
        // 今日票房
        mav.addObject("iscommercial", moviesService.maxList());
        // 获取最新的三条新闻
        mav.addObject("newLists", newsService.newListThree());
    }


    /**
     * 首页-搜索
     * @param moviename
     * @return
     */
    @RequestMapping("/result")
    public ModelAndView result(String moviename){
        ModelAndView mav = new ModelAndView();
        PageHelper.startPage(1,5);
        List<Movies> moviesList = moviesService.list(moviename);
        mav.addObject("moviesList",moviesList);
        aside(mav);
        mav.setViewName("front/result.jsp");
        return mav;
    }
}
